package com.starbucks.opencase.construct;

import com.starbucks.opencase.data.daFriendMessage;
import com.starbucks.opencase.data.daSession;
import com.starbucks.opencase.enums.enumCMKey;
import com.starbucks.opencase.function.funFriendMessage;
import com.starbucks.opencase.function.funSession;
import com.starbucks.opencase.modualr.moFriendMessage;
import com.starbucks.opencase.tool.toolProperties;
import jakarta.annotation.PostConstruct;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.ArrayList;
import java.util.Timer;
import java.util.TimerTask;

@Component
public class constructFriendMessage {
    @Autowired
    private funFriendMessage funFriendMessage;
    @Autowired
    private funSession funSession;
    @Autowired
    private toolProperties toolProperties;
    /**
     * 计算消息超时
     * 每天晚上计算
     */
    @PostConstruct
    public void testPastDue(){
        LocalDate today=LocalDate.now();
        today.plusDays(1);
        LocalDateTime t=today.atTime(0,0,0,0);
        long tt=t.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();
        new Timer().schedule(new count(),Math.max(0,tt-System.currentTimeMillis()));
    }
    @PostConstruct
    public void longSendMessage(){
        new Timer().schedule(new loginSend(),5000);
    }
    public class count extends TimerTask{

        @Override
        public void run() {
            long n=System.currentTimeMillis();
            long d= (long) toolProperties.queryInfo().getMessageCacheDay() *24*60*60*1000;
            ArrayList<Long> re=new ArrayList<>();
            for(Long key: daFriendMessage.message.keySet())if(key+d<n)re.add(key);
            for(Long k:re)funFriendMessage.removeMessage(k);
            testPastDue();
        }
    }

    /**
     * 用户登录了，发送过去
     */
    public class loginSend extends TimerTask{
        @Override
        public void run() {
            LocalDate today=LocalDate.now();
            LocalDateTime today_t=today.atTime(0,0,0,0);
            long t1=today_t.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();
            today_t=today_t.plusDays(1);
            long t2=today_t.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();
            if(Math.abs(t1-System.currentTimeMillis())<360_000 || Math.abs(t2-System.currentTimeMillis())<360_000){
                longSendMessage();
                return;
            }

            for(String sid:daSession.client_users.keySet()){
                String uid=daSession.client_users.get(sid);
                ArrayList<moFriendMessage> qw=new ArrayList<>();
                for(Long k:daFriendMessage.message.keySet()){
                    moFriendMessage mo=daFriendMessage.message.get(k);
                    if(mo.getMessage().get(enumCMKey.type.name()).asInt()==0 && mo.getRecipient().equals(uid))qw.add(mo);
                }
                for(moFriendMessage mo:qw){
                    funFriendMessage.removeMessage(mo.getTimestamp());
                    long a=funFriendMessage.send(mo.getSender(),mo.getRecipient(),mo.getMessage());
                    funFriendMessage.removeMessage(a);
                }
            }
            longSendMessage();
        }
    }
}
